#include "NetworkMessenger.h"

#include <cstdlib>
#include <time.h>
#include <limits>

void ClientMessenger::JoinGame(NetworkMessage& _netMsg)
{
	_netMsg.WriteByte(CL_CONNECT);
}

void ClientMessenger::Broadcast(NetworkMessage& _netMsg)
{
	_netMsg.WriteByte(CL_BROADCAST);
}

void ClientMessenger::PlayerMove(NetworkMessage& _netMsg, Event* _event)
{
	_netMsg.WriteByte(CL_PLAYER_MOVE);
	_netMsg.WriteData(_event->GetParam(), sizeof(PlayerMoveEvent));
}

void ServerMessenger::Connect(NetworkMessage& _netMsg)
{
	_netMsg.WriteByte(SV_CONNECT);
}

void ServerMessenger::Full(NetworkMessage& _netMsg)
{
	_netMsg.WriteByte(SV_FULL);
}

bool IsSequenceMoreRecent(unsigned char _sequenceNumber1, unsigned char _sequenceNumber2)
{
	return (_sequenceNumber1 > _sequenceNumber2) && (_sequenceNumber1 - _sequenceNumber2 <= UCHAR_MAX/2)
		|| (_sequenceNumber2 > _sequenceNumber1) && (_sequenceNumber2 - _sequenceNumber1 > UCHAR_MAX/2);
}

void WriteMessageHeader(NetworkMessage& _netMsg, NetworkHeader& _netHeader)
{
	_netMsg.WriteData(&_netHeader, sizeof(NetworkHeader));
	++_netHeader.sequenceNumber;
}